<template>
    <div class="body1">
        <div class="no-print el-button-print">
            <el-button @click="exportPDF">导出为pdf</el-button>
        </div>

        <div class="body2" id="pdfDom" :class="{ 'pdf-export': isPdfExport }">
            <div class="title">
                <div class="biaotididian">{{ agencyName }}</div>
                <div class="biaoti">案件调查终结报告</div>
            </div>
            <div class="tableStyle1">
                <div class="writ-height">
                    <span>&emsp;&emsp;一、案件来源</span>
                </div>
                <div style="height: 185px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a1" :rows="a1Config.maxLines"
                        @keydown.native="handleKeyDown('a1', $event)" @input="handleInput('a1', $event)"
                        @paste.native="handlePaste('a1', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div style="height: 120px; line-height: 30px;">
                    <span>&emsp;&emsp;二、立案情况</span>
                    <br>
                    <span>&emsp;&emsp;</span>
                    <editable-textarea :text="form.date1" param-type="YYYYMMDD1" @send-to-parent="receiveFromChild" />
                    <span>，经​</span>
                    <input v-model="form.a2" :resize="'none'" autosize maxlength="10" class="inputStyle1"
                        style="width: calc(10 * 5mm);font-family: 仿宋;font-size: 5mm;" type="textarea">
                    <span>批准，对</span>
                    <CrossRowInput v-model:value="form.a3" :line-config="a3Config" />
                    <span>涉嫌</span>
                    <CrossRowInput v-model:value="form.a4" :line-config="a4Config" />
                    <span>一案予以立案。</span>
                </div>
                <div style="height: 180px; line-height: 30px;">
                    <span>&emsp;&emsp;三、当事人基本情况</span>
                    <br>
                    <div style="display: inline-block; position: relative;">
                        <span>&emsp;&emsp;当事人姓名/名称：</span>
                        <privy-info-select-return-array v-model="form.unitName" v-model:isSelected="form.isUnit"
                            :isTable="false" return-fields="A"
                            :return-array-template="[{ name: 'legalPerson', field: 'C', }, { name: 'companyLicense', field: 'B', }, { name: 'companyAddress', field: 'E', }, { name: 'businessNature', field: 'G', },]"
                            :rowConfig="unitNameConfig" @select="selectUnitUpdateData">
                        </privy-info-select-return-array>
                    </div>
                    <br>
                    <span>&emsp;&emsp;法定代表人/负责人：</span>
                    <CrossRowInput v-model:value="form.legalPerson" :line-config="legalPersonConfig" />
                    <br>
                    <span>&emsp;&emsp;统一社会信用代码/身份证号码：</span>
                    <CrossRowInput v-model:value="form.companyLicense" :line-config="companyLicenseConfig" />
                    <br>
                    <span>&emsp;&emsp;地址：</span>
                    <CrossRowInput v-model:value="form.companyAddress" :line-config="companyAddressConfig" />
                    <br>
                    <span>&emsp;&emsp;经营范围/职业：</span>
                    <CrossRowInput v-model:value="form.businessNature" :line-config="businessNatureConfig" />
                </div>
                <div style="height: 120px; line-height: 30px;">
                    <span>&emsp;&emsp;四、违法事实​</span>
                    <br>
                    <span>&emsp;&emsp;违法行为发生时间：</span>
                    <editable-textarea :text="form.date2" param-type="YYYYMMDD2" @send-to-parent="receiveFromChild" />
                    <span>至</span>
                    <editable-textarea :text="form.date3" param-type="YYYYMMDD3" @send-to-parent="receiveFromChild" />
                    <span>期间</span>
                    <br>
                    <span>&emsp;&emsp;违法行为发生地点：</span>
                    <CrossRowInput v-model:value="form.a10" :line-config="a10Config" />
                    <br>
                    <span>&emsp;&emsp;违法行为的具体表现：</span>
                </div>
                <div style="height: 215px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a11" :rows="a11Config.maxLines"
                        placeholder="详细描述当事人实施违法行为的手段、方式、过程等，例如，在 [具体生产环节]，当事人 [具体违规操作]，导致 [产生的不良后果或影响]。"
                        @keydown.native="handleKeyDown('a11', $event)" @input="handleInput('a11', $event)"
                        @paste.native="handlePaste('a11', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
            </div>
            <div class="page-break"
                style="border-collapse: collapse;border: none;font-weight: normal;font-size: 5mm;width: 100%;">
                <div class="writ-height">
                    <span>&emsp;&emsp;经调查核实：</span>
                </div>
                <div style="height: 155px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a12" :rows="a12Config.maxLines"
                        placeholder="当事人在 [相关业务活动中]，[违反规定的具体行为细节]，如 [列举具体违反的行业标准、操作规范等内容]。"
                        @keydown.native="handleKeyDown('a12', $event)" @input="handleInput('a12', $event)"
                        @paste.native="handlePaste('a12', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div style="height: 60px; line-height: 30px;">
                    <span>&emsp;&emsp;违法所得：经核算，当事人因该违法行为获得违法所得共计人民币</span>
                    <CrossRowInput v-model:value="form.a13" :line-config="a13Config" />
                    <span>元。计算方式为</span>
                </div>
                <div style="height: 95px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a14" :rows="a14Config.maxLines"
                        placeholder="[详细说明违法所得的计算依据和过程]。" @keydown.native="handleKeyDown('a14', $event)"
                        @input="handleInput('a14', $event)" @paste.native="handlePaste('a14', $event)"
                        class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;危害后果：</span>
                </div>
                <div style="height: 185px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a15" :rows="a15Config.maxLines"
                        placeholder="该违法行为对 [具体的受影响对象或领域，如环境、消费者权益、市场秩序等] 造成了 [详细阐述危害后果的表现和程度]。例如，[描述对环境造成的污染情况、对消费者造成的经济损失或人身伤害等]。"
                        @keydown.native="handleKeyDown('a15', $event)" @input="handleInput('a15', $event)"
                        @paste.native="handlePaste('a15', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;五、调查经过</span>
                </div>
                <div style="height: 60px; line-height: 30px;">
                    <span>&emsp;&emsp;办案人员组成：由</span>
                    <input v-model="form.a16" :resize="'none'" autosize maxlength="7" class="inputStyle1"
                        placeholder="执法人员姓名1" style="width: calc(7 * 5mm);font-family: 仿宋;font-size: 5mm;"
                        type="textarea">、
                    <input v-model="form.a17" :resize="'none'" autosize maxlength="7" class="inputStyle1"
                        placeholder="执法人员姓名2" style="width: calc(7 * 5mm);font-family: 仿宋;font-size: 5mm;"
                        type="textarea">
                    <span>等组成办案小组，负责本案的调查工作。</span>
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;调查方式：</span>
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a18">现场检查：</span>
                </div>
                <div style="height: 245px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a19" :rows="a19Config.maxLines"
                        placeholder="[具体检查时间]，办案人员依法对当事人的 [经营场所 / 生产场地等] 进行现场检查，制作了《现场检查（勘察）笔录》，记录了现场发现的 [违法物品、设备状态、操作情况等相关情况]，并拍摄了现场照片 [X] 张作为证据。"
                        @keydown.native="handleKeyDown('a19', $event)" @input="handleInput('a19', $event)"
                        @paste.native="handlePaste('a19', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
            </div>
            <div class="page-break"
                style="border-collapse: collapse;border: none;font-weight: normal;font-size: 5mm;width: 100%;">
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a20">调查询问：</span>
                </div>
                <div style="height: 245px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a21" :rows="a21Config.maxLines"
                        placeholder="[具体询问时间]，办案人员对当事人的 [法定代表人 / 负责人 / 相关责任人] 进行了调查询问，制作了《调查询问笔录》[X] 份。在询问过程中，当事人对 [相关违法事实] 予以承认，并对 [涉及违法的相关问题] 进行了陈述和说明。"
                        @keydown.native="handleKeyDown('a21', $event)" @input="handleInput('a21', $event)"
                        @paste.native="handlePaste('a21', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a22">收集书证物证：</span>
                </div>
                <div style="height: 155px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a23" :rows="a23Config.maxLines"
                        placeholder="办案人员收集了与本案相关的书证物证，包括 [列举书证物证的名称、来源及证明内容，如合同、发票、生产记录、产品样本等]。"
                        @keydown.native="handleKeyDown('a23', $event)" @input="handleInput('a23', $event)"
                        @paste.native="handlePaste('a23', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a24">其他调查手段：</span>
                </div>
                <div style="height: 215px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a25" :rows="a25Config.maxLines"
                        placeholder="[如有其他调查方式，如委托鉴定、向相关部门协查等，在此说明]。" @keydown.native="handleKeyDown('a25', $event)"
                        @input="handleInput('a25', $event)" @paste.native="handlePaste('a25', $event)"
                        class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;六、相关证据及证明事项</span>
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a26">《现场检查（勘察）笔录》：</span>
                </div>
                <div style="height: 185px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a27" :rows="a27Config.maxLines"
                        placeholder="证明了 [具体证明内容，如现场存在违法物品的数量、摆放情况，生产设备的运行状态，作业环境的违规情况等]。"
                        @keydown.native="handleKeyDown('a27', $event)" @input="handleInput('a27', $event)"
                        @paste.native="handlePaste('a27', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
            </div>
            <div class="page-break"
                style="border-collapse: collapse;border: none;font-weight: normal;font-size: 5mm;width: 100%;">
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a28">《调查询问笔录》：</span>
                </div>
                <div style="height: 185px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a29" :rows="a29Config.maxLines"
                        placeholder="证明当事人在笔录中承认了 [具体违法事实]，并对 [违法的动机、目的、过程等相关情况] 进行了陈述，该笔录为认定当事人违法行为提供了重要的当事人陈述证据。"
                        @keydown.native="handleKeyDown('a29', $event)" @input="handleInput('a29', $event)"
                        @paste.native="handlePaste('a29', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a30">现场照片：</span>
                </div>
                <div style="height: 185px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a31" :rows="a31Config.maxLines"
                        placeholder="照片 [编号 1] 显示了 [照片反映的具体违法场景或物品特征等]，照片 [编号 2]……，这些照片直观地记录了现场情况，与《现场检查（勘察）笔录》相互印证，证明了当事人违法行为的存在。"
                        @keydown.native="handleKeyDown('a31', $event)" @input="handleInput('a31', $event)"
                        @paste.native="handlePaste('a31', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a32">书证物证：</span>
                </div>
                <div style="height: 245px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a33" :rows="a33Config.maxLines"
                        placeholder="[书证名称 1]，证明了 [其证明的与违法事实相关的内容，如当事人的交易往来记录、涉及违法产品的生产批次等]。​[物证名称 1]，该物证 [描述物证的特征和与违法事实的关联，如物证为违法生产的产品样本，其质量、规格等不符合相关标准，直接证明了产品质量违法问题]。"
                        @keydown.native="handleKeyDown('a33', $event)" @input="handleInput('a33', $event)"
                        @paste.native="handlePaste('a33', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a34">其他证据：</span>
                </div>
                <div style="height: 215px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a35" :rows="a35Config.maxLines" placeholder="[说明其他证据的证明事项]。"
                        @keydown.native="handleKeyDown('a35', $event)" @input="handleInput('a35', $event)"
                        @paste.native="handlePaste('a35', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
            </div>
            <div class="page-break"
                style="border-collapse: collapse;border: none;font-weight: normal;font-size: 5mm;width: 100%;">
                <div class="writ-height">
                    <span>&emsp;&emsp;七、定性分析</span>
                </div>
                <div style="height: 305px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a36" :rows="a36Config.maxLines"
                        placeholder="根据查明的事实和证据，当事人的行为违反了 [具体法律法规名称及条款] 的规定。该条款明确规定 [引用法律法规条款原文内容]，而当事人的 [具体违法行为] 与该条款所禁止的行为特征相符。因此，认定当事人的行为构成了 [违法行为的定性表述]。"
                        @keydown.native="handleKeyDown('a36', $event)" @input="handleInput('a36', $event)"
                        @paste.native="handlePaste('a36', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;八、处罚依据及裁量权适用情况</span>
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;处罚依据：</span>
                </div>
                <div style="height: 245px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a37" :rows="a37Config.maxLines"
                        placeholder="依据 [具体法律法规名称及条款] 的规定，[引用对该违法行为进行处罚的相关法律条文原文]，对当事人的违法行为应当给予行政处罚。"
                        @keydown.native="handleKeyDown('a37', $event)" @input="handleInput('a37', $event)"
                        @paste.native="handlePaste('a37', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;裁量权适用情况：</span>
                </div>
                <div style="height: 275px; width: 161mm;">
                    <el-input type="textarea" v-model="form.a38" :rows="a38Config.maxLines"
                        placeholder="考虑到当事人在本次违法行为中的 [从轻 / 从重 / 一般情节的具体表现，如积极配合调查、主动消除危害后果、多次违法等]，根据本地区关于 [相关法律法规] 的行政处罚裁量基准，[说明裁量的具体依据和理由]，建议对当事人给予 [具体的行政处罚种类和幅度] 的处罚。"
                        @keydown.native="handleKeyDown('a38', $event)" @input="handleInput('a38', $event)"
                        @paste.native="handlePaste('a38', $event)" class="el-textarea-hidden"
                        style=" height: 100%;white-space: pre;overflow-x: auto;font-family: monospace;" wrap="soft" />
                </div>
            </div>
            <div class="page-break"
                style="border-collapse: collapse;border: none;font-weight: normal;font-size: 5mm;width: 100%;">
                <div class="writ-height">
                    <span>&emsp;&emsp;九、处罚建议</span>
                </div>
                <div class="writ-height">
                    <span>&emsp;&emsp;行政处罚种类：</span>
                </div>
                <div style="height: 160px; line-height: 30px;">
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a39" />罚款：建议对当事人处以人民币</span>
                    <CrossRowInput v-model:value="form.a40" :line-config="a40Config" />
                    <span>元的罚款。</span>
                    <br>
                    <span>&emsp;&emsp;<input type="checkbox" v-model="form.a41" />其他处罚种类：</span>
                    <CrossRowInput v-model:value="form.a42" :line-config="a42Config" />
                </div>
                <div style="height: 150px; line-height: 30px;">
                    <span>&emsp;&emsp;履行方式和期限：当事人应在收到本行政处罚决定书之日起</span>
                    <input v-model="form.a43" :resize="'none'" autosize maxlength="3" class="inputStyle1"
                        style="width: calc(3 * 5mm);font-family: 仿宋;font-size: 5mm;" type="textarea">
                    <span>日内，将罚款缴纳至</span>
                    <CrossRowInput v-model:value="form.a44" :line-config="a44Config" />
                    <span>。逾期不缴纳罚款的，根据《中华人民共和国行政处罚法》第七十二条第一款第（一）项的规定，每日按罚款数额的百分之三加处罚款。​</span>
                </div>
                <div style="height: 120px; line-height: 30px;">
                    <span>&emsp;&emsp;告知事项：如当事人不服本处罚决定，可在收到本处罚决定书之日起六十日内向</span>
                    <input v-model="form.a45" :resize="'none'" autosize maxlength="25" class="inputStyle1"
                        style="width: calc(25 * 5mm);font-family: 仿宋;font-size: 5mm;" type="textarea">
                    <span>申请行政复议，也可在六个月内直接向</span>
                    <input v-model="form.a46" :resize="'none'" autosize maxlength="15" class="inputStyle1"
                        style="width: calc(15 * 5mm);font-family: 仿宋;font-size: 5mm;" type="textarea">
                    <span>人民法院提起行政诉讼。行政复议和行政诉讼期间，本处罚决定不停止执行。​​</span>
                </div>
                <div :style="{
                    'height': '400px',
                    'display': isPdfExport ? 'block' : 'none'
                }">
                    <!-- 控制签名在最下方 -->
                </div>
                <div style="position: relative; height: 60px;">
                    <div
                        style="position: absolute; bottom: 10px; left: 0px; right: 20px; display: flex; justify-content: space-between; align-items: flex-end; ">
                        <div style="display: flex; align-items: flex-end;">
                            <span>办案人员签名：</span>
                            <span style="border-bottom: 1px solid black; padding-bottom: 6px;">
                                <SignatureField label="签名1" sign-type="sign1" :img-url="form.img1"
                                    @update:imgUrl="form.img1 = $event" />
                            </span>
                            ，
                            <span style="border-bottom: 1px solid black; padding-bottom: 6px;">
                                <SignatureField label="签名2" sign-type="sign2" :img-url="form.img2"
                                    @update:imgUrl="form.img2 = $event" />
                            </span>
                        </div>
                        <div>
                            <editable-textarea :text="form.date4" param-type="YYYYMMDD4"
                                @send-to-parent="receiveFromChild" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import EditableTextarea from "@/views/template/EditableTextarea";
import { agencyName, sealName } from "../../utils/publicName.js"
import PrivyInfoSelectReturnArray from "@/views/template/PrivyInfoSelectReturnArray";
import LawsAndRegulationsSelect from "@/views/template/LawsAndRegulationsSelect";
import CrossRowInput from "@/views/template/CrossRowInput";
import SignatureField from "@/views/template/SignatureField";
import { createInputLimiter } from '@/utils/limitInput';

export default {
    components: {
        LawsAndRegulationsSelect,
        EditableTextarea,
        CrossRowInput,
        SignatureField,
        PrivyInfoSelectReturnArray,
    },

    data() {
        return {
            agencyName: agencyName,
            sealName: sealName,
            form: {
                isUnit: true,
            },
            status: '', //当前文书的状态
            isPdfExport: false, //控制打印样式
            // isDisplay: true, //控制官方印章的显示
            a3Config: {
                firstLine: { chars: 7 * 2 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 13 * 2 }
            },
            a4Config: {
                firstLine: { chars: 17 * 2 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 21 * 2 }
            },
            unitNameConfig: {
                firstLine: { chars: 21 * 2 + 1 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 0 * 2 }
            },
            legalPersonConfig: {
                firstLine: { chars: 20 * 2 + 1 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 0 * 2 }
            },
            companyLicenseConfig: {
                firstLine: { chars: 15 * 2 + 1 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 0 * 2 }
            },
            companyAddressConfig: {
                firstLine: { chars: 27 * 2 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 0 * 2 }
            },
            businessNatureConfig: {
                firstLine: { chars: 22 * 2 + 1 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 0 * 2 }
            },
            a10Config: {
                firstLine: { chars: 21 * 2 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 0 * 2 }
            },
            a13Config: {
                firstLine: { chars: 1 * 2 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 25 * 2 }
            },
            a40Config: {
                firstLine: { chars: 15 * 2 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 10 * 2 }
            },
            a42Config: {
                firstLine: { chars: 22 * 2 },
                middleLines: { count: 1, chars: 32 * 2 },
                lastLine: { chars: 32 * 2 }
            },
            a44Config: {
                firstLine: { chars: 24 * 2 },
                middleLines: { count: 0, chars: 32 * 2 },
                lastLine: { chars: 16 * 2 }
            },
            a45Config: {
                firstLine: { chars: 22 * 2 },
                middleLines: { count: 1, chars: 32 * 2 },
                lastLine: { chars: 32 * 2 }
            },
            a1Config: {
                maxLines: 6,
                charsPerLine: 32 * 2,
                maxLength: 6 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a11Config: {
                maxLines: 7,
                charsPerLine: 32 * 2,
                maxLength: 7 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a12Config: {
                maxLines: 5,
                charsPerLine: 32 * 2,
                maxLength: 5 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a14Config: {
                maxLines: 3,
                charsPerLine: 32 * 2,
                maxLength: 3 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a15Config: {
                maxLines: 6,
                charsPerLine: 32 * 2,
                maxLength: 6 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a19Config: {
                maxLines: 8,
                charsPerLine: 32 * 2,
                maxLength: 8 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a21Config: {
                maxLines: 8,
                charsPerLine: 32 * 2,
                maxLength: 8 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a23Config: {
                maxLines: 5,
                charsPerLine: 32 * 2,
                maxLength: 5 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a25Config: {
                maxLines: 7,
                charsPerLine: 32 * 2,
                maxLength: 7 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a27Config: {
                maxLines: 6,
                charsPerLine: 32 * 2,
                maxLength: 6 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a29Config: {
                maxLines: 6,
                charsPerLine: 32 * 2,
                maxLength: 6 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a31Config: {
                maxLines: 6,
                charsPerLine: 32 * 2,
                maxLength: 6 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a32Config: {
                maxLines: 6,
                charsPerLine: 32 * 2,
                maxLength: 6 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a33Config: {
                maxLines: 8,
                charsPerLine: 32 * 2,
                maxLength: 8 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a35Config: {
                maxLines: 7,
                charsPerLine: 32 * 2,
                maxLength: 7 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a36Config: {
                maxLines: 10,
                charsPerLine: 32 * 2,
                maxLength: 10 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a37Config: {
                maxLines: 8,
                charsPerLine: 32 * 2,
                maxLength: 8 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
            a38Config: {
                maxLines: 9,
                charsPerLine: 32 * 2,
                maxLength: 9 * 32 * 2,
                inputLimiter: null,
                displayText: ''
            },
        }
    },

    created() {
        window.parent.getTemplateData((data, status) => {
            this.form = data;
            this.status = status;
        })
        window.postForm = () => {
            this.postForm();
        }
        this.a1Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a1Config.maxLines,
            charsPerLine: this.a1Config.charsPerLine,
            maxLength: this.a1Config.maxLength,
            propName: `form.a1`
        });
        this.a11Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a11Config.maxLines,
            charsPerLine: this.a11Config.charsPerLine,
            maxLength: this.a11Config.maxLength,
            propName: `form.a11`
        });
        this.a12Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a12Config.maxLines,
            charsPerLine: this.a12Config.charsPerLine,
            maxLength: this.a12Config.maxLength,
            propName: `form.a12`
        });
        this.a14Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a14Config.maxLines,
            charsPerLine: this.a14Config.charsPerLine,
            maxLength: this.a14Config.maxLength,
            propName: `form.a14`
        });
        this.a15Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a15Config.maxLines,
            charsPerLine: this.a15Config.charsPerLine,
            maxLength: this.a15Config.maxLength,
            propName: `form.a15`
        });
        this.a19Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a19Config.maxLines,
            charsPerLine: this.a19Config.charsPerLine,
            maxLength: this.a19Config.maxLength,
            propName: `form.a19`
        });
        this.a21Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a21Config.maxLines,
            charsPerLine: this.a21Config.charsPerLine,
            maxLength: this.a21Config.maxLength,
            propName: `form.a21`
        });
        this.a23Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a23Config.maxLines,
            charsPerLine: this.a23Config.charsPerLine,
            maxLength: this.a23Config.maxLength,
            propName: `form.a23`
        });
        this.a25Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a25Config.maxLines,
            charsPerLine: this.a25Config.charsPerLine,
            maxLength: this.a25Config.maxLength,
            propName: `form.a25`
        });
        this.a27Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a27Config.maxLines,
            charsPerLine: this.a27Config.charsPerLine,
            maxLength: this.a27Config.maxLength,
            propName: `form.a27`
        });
        this.a29Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a29Config.maxLines,
            charsPerLine: this.a29Config.charsPerLine,
            maxLength: this.a29Config.maxLength,
            propName: `form.a29`
        });
        this.a31Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a31Config.maxLines,
            charsPerLine: this.a31Config.charsPerLine,
            maxLength: this.a31Config.maxLength,
            propName: `form.a31`
        });
        this.a33Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a33Config.maxLines,
            charsPerLine: this.a33Config.charsPerLine,
            maxLength: this.a33Config.maxLength,
            propName: `form.a33`
        });
        this.a35Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a35Config.maxLines,
            charsPerLine: this.a35Config.charsPerLine,
            maxLength: this.a35Config.maxLength,
            propName: `form.a35`
        });
        this.a36Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a36Config.maxLines,
            charsPerLine: this.a36Config.charsPerLine,
            maxLength: this.a36Config.maxLength,
            propName: `form.a36`
        });
        this.a37Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a37Config.maxLines,
            charsPerLine: this.a37Config.charsPerLine,
            maxLength: this.a37Config.maxLength,
            propName: `form.a37`
        });
        this.a38Config.inputLimiter = createInputLimiter(this, {
            maxLines: this.a38Config.maxLines,
            charsPerLine: this.a38Config.charsPerLine,
            maxLength: this.a38Config.maxLength,
            propName: `form.a38`
        });
    },

    mounted() {
        window.parent.changeTemplateDow();
    },

    methods: {
        getConfigName(field) {
            switch (field) {
                case 'a1':
                    return this.a1Config;
                case 'a11':
                    return this.a11Config;
                case 'a12':
                    return this.a12Config;
                case 'a14':
                    return this.a14Config;
                case 'a15':
                    return this.a15Config;
                case 'a19':
                    return this.a19Config;
                case 'a21':
                    return this.a21Config;
                case 'a23':
                    return this.a23Config;
                case 'a25':
                    return this.a25Config;
                case 'a27':
                    return this.a27Config;
                case 'a29':
                    return this.a29Config;
                case 'a31':
                    return this.a31Config;
                case 'a33':
                    return this.a33Config;
                case 'a35':
                    return this.a35Config;
                case 'a36':
                    return this.a36Config;
                case 'a37':
                    return this.a37Config;
                case 'a38':
                    return this.a38Config;
            }
        },
        handleKeyDown(field, e) {
            const config = this.getConfigName(field);
            config.inputLimiter.handleKeyDown(e);
        },
        handleInput(field, value) {
            const config = this.getConfigName(field);
            this.form[field] = config.inputLimiter.handleInput(value);
        },
        handlePaste(field, e) {
            const config = this.getConfigName(field);
            config.inputLimiter.handlePaste(e);
        },
        // 导出为pdf
        exportPDF() {
            // if (this.status !== '4') {
            //     this.isDisplay = false
            // }
            this.savePDF()
                .then((blob) => {

                    const blobUrl = URL.createObjectURL(blob);

                    // 先预览
                    const previewWindow = window.open(blobUrl, '_blank');

                    // 自动下载
                    const fileName = `案件调查终结报告.pdf`;
                    const downloadLink = document.createElement('a');
                    downloadLink.href = blobUrl;
                    downloadLink.download = fileName;
                    document.body.appendChild(downloadLink);
                    downloadLink.click();
                    document.body.removeChild(downloadLink);

                    // 监听预览窗口关闭后释放内存
                    if (previewWindow) {
                        const timer = setInterval(() => {
                            if (previewWindow.closed) {
                                clearInterval(timer);
                                URL.revokeObjectURL(blobUrl);
                            }
                        }, 1000);
                    } else {
                        setTimeout(() => URL.revokeObjectURL(blobUrl), 10000); // 兜底释放
                    }
                })
                .catch(error => {
                    console.error('导出PDF出错:', error);
                    alert('导出失败，请重试或联系管理员');
                });
        },
        savePDF() {
            this.isPdfExport = true; // 激活PDF样式
            return new Promise((resolve, reject) => {
                this.$nextTick(() => {
                    import('html2pdf.js').then((module) => {
                        const html2pdf = module.default;

                        //获取所有需要替换的 el-input
                        const elInputs = document.querySelectorAll('.el-textarea-hidden .el-input__inner, .el-textarea-hidden .el-textarea__inner');

                        //存储原始元素和它们的父节点，方便后续恢复
                        const originalElements = [];

                        elInputs.forEach((input) => {
                            const div = document.createElement('div');
                            div.style.whiteSpace = 'pre-wrap'; // 保留换行
                            div.style.fontFamily = '仿宋';
                            div.style.fontSize = '19px';
                            div.style.height = '100%';
                            div.style.width = '100%';

                            div.textContent = input.value; // 保留原内容（自动处理 \n 换行）
                            div.style.lineHeight = '30px';
                            div.style.backgroundImage = 'linear-gradient(to top, #000 1px, transparent 1px)';
                            div.style.backgroundSize = '100% 30px';
                            div.style.backgroundRepeat = 'repeat-y';
                            div.style.backgroundPosition = '0 -3px';
                            div.style.padding = '0';
                            div.style.boxSizing = 'border-box';


                            // 替换 el-input，并存储原始元素
                            originalElements.push({
                                parent: input.parentNode,
                                original: input,
                                replacement: div,
                            });
                            input.parentNode.replaceChild(div, input);
                        });

                        // 处理选择执法人的替换
                        const InvestigatorSelectSelects = document.querySelectorAll('.InvestigatorSelectReplace .el-input__inner, .InvestigatorSelectReplace .el-textarea__inner');
                        InvestigatorSelectSelects.forEach((input) => {
                            const div = document.createElement('div');
                            div.style.whiteSpace = 'pre-wrap'; // 保留换行
                            div.style.fontFamily = '仿宋';
                            div.style.fontSize = '5mm';
                            div.textContent = input.value; // 保留原内容（自动处理 \n 换行）
                            div.style.lineHeight = '1.3';

                            // 替换 el-input，并存储原始元素
                            originalElements.push({
                                parent: input.parentNode,
                                original: input,
                                replacement: div,
                            });
                            input.parentNode.replaceChild(div, input);
                        });

                        // 配置选项
                        const opt = {
                            margin: 0,
                            image: {
                                type: 'jpeg',
                                quality: 0.98
                            },
                            html2canvas: {
                                scale: 2,
                                logging: false,
                                useCORS: true
                            },
                            jsPDF: {
                                unit: 'mm',
                                format: 'a4',
                                orientation: 'portrait'
                            }
                        };

                        const element = document.getElementById('pdfDom');

                        // 生成PDF Blob对象
                        html2pdf()
                            .set(opt)
                            .from(element)
                            .toPdf()
                            .get('pdf')
                            .then((pdf) => {
                                const blob = pdf.output('blob');

                                resolve(blob); // 返回Blob对象
                            })
                            .catch((error) => {
                                reject(error);
                            })
                            .finally(() => {
                                this.isPdfExport = false; // 恢复原始样式
                                // 恢复原始input
                                originalElements.forEach(({ parent, original, replacement }) => {
                                    parent.replaceChild(original, replacement);
                                });
                            });
                    }).catch((error) => {
                        reject(error);
                        this.isPdfExport = false;
                        originalElements.forEach(({ parent, original, replacement }) => {
                            parent.replaceChild(original, replacement);
                        });
                    });
                });
            });
        },
        //父页面提交按钮将调用此方法，此方法将文书数据回传到父页面的请求方法中
        postForm() {
            this.savePDF()
                .then((blob) => {


                    // 创建FormData并添加文件
                    const formData = new FormData();
                    formData.append('file', blob, '案件调查终结报告.pdf');

                    window.parent.postTemplateDate(this.form, formData);
                })
                .catch(error => {
                    console.error('整个保存PDF出错:', error);
                });
        },
        //选择日期时间
        receiveFromChild(data, type) {
            const TypeMap = {
                'YYYYMMDD1': 'date1',
                'YYYYMMDD2': 'date2',
                'YYYYMMDD3': 'date3',
                'YYYYMMDD4': 'date4',
            };
            if (TypeMap[type]) {
                this.form[TypeMap[type]] = data;
            }
        },
        //选择执法人员
        handleInvestigatorChange({ identificationNumber, idField }) {
            if (idField && identificationNumber) {
                this.form[idField] = identificationNumber;
            }
        },
        // 选择单位
        selectUnitUpdateData(data) {
            data.forEach(item => {
                this.form[item.name] = item.value;
            })
        },
    }
}
</script>

<style scoped>
:deep(.el-button-print .el-button) {
    display: inline-flex !important;
    visibility: visible !important;
    opacity: 1 !important;
    width: auto;
    min-width: 100px;
    height: auto;
    padding: 8px 15px;
    background: #fff !important;
    color: #000 !important;
    border: none;
}

:deep(.el-textarea__inner) {
    box-shadow: 0 0 0 0;
    color: black;
    padding: 0;
    font-family: "仿宋", serif;
    font-size: 5mm;
    overflow-y: hidden;
    line-height: 1.6;

    /* 下划线 */
    border-radius: 0px;
    background-image: linear-gradient(to top, #000000 0.5px, transparent 1px);
    background-size: 100% 1.6em;
    background-position: 0 -3px;
}

:deep(.el-textarea-hidden .el-textarea__inner) {
    box-shadow: 0 0 0 0;
    color: black;
    padding: 0;
    font-family: "仿宋", serif;
    font-size: 5mm;
    overflow-y: hidden !important;
    resize: none !important;
    height: auto !important;
}

:deep(.el-select .el-input__inner) {
    text-align: center;
    height: 5mm;
}

.input-container {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
}

.inputStyleNoLine {
    border: none;
    outline: none;
    padding: 0px;
}

.typeSpan {
    cursor: pointer;
    font-size: 5mm;
}
</style>
